const mysql = require('mysql');
const config = require('../config');
const logger = require('../logs/logger');

// privates 

var _pool = mysql.createPool(config.mysql);

// privates

module.exports = {
    get pool() {
        if (_pool) {
            return _pool;
        }

        return _pool = mysql.createPool(config.mysql);
    },

    execute(action) {
        this.pool.getConnection((err, connection) => {
            if (err) {
                logger.exception(err);
                return;
            }
            action(connection);

            connection.release();
        });
    }
};